<?php
declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Nats\Consumer;
use Hyperf\Nats\AbstractConsumer;
use Hyperf\Nats\Annotation\Consumer;
use Hyperf\Nats\Message;
use App\Utils\ServerUtil;
use App\Logic\Amount;
/**
 *
 */
class AmountConsumer extends AbstractConsumer
{
    public function consume(Message $payload)
    {
        $data = $payload->getBody(); 
        $data = json_decode($data,true); 
        ServerUtil::info('Nats-AmountConsumer-进入', ['param'=>$data]);
        $type = $data['type'] ?? '';
        if(!\is_array($data)||empty($type)){
            $payload->reply(json_encode(ServerUtil::nats(1001,'请求参数不对')));  
            return;
        }
        $obj = $this->container->get(Amount::class);
        //开协程处理,防止进程阻塞下个逻辑
        co(function () use ($obj,$payload,$data,$type){
            switch ($type)
            {
                case 1: //校验金额是否合法
                    $res = $obj->isLegalDisCountAmount($data['ori_amount'],$data['amount'],$data['type_id']);
                    ServerUtil::info('Nats-AmountConsumer-校验金额是否合法',['param'=>$data,'res'=>$res]);
                    if(!$res){
                        $payload->reply(json_encode(ServerUtil::nats(600,'金额不合法',$res)));  
                    }else{
                        $payload->reply(json_encode(ServerUtil::nats(0,'金额合法',$res)));  
                    }
                    break;
                default:
                $payload->reply(json_encode(ServerUtil::nats(600,'type解析失败'))); 
            }
        });
        return;
    }
}